﻿using System;

namespace SoftwareConsulting.BI11.NumericalMethods.Integration
{    
    /// <summary>
    /// Класс метода трапеций
    /// </summary>
    public class TrapeziumIntegration
    {
        private double[,] _result;

        /// <summary>
        /// Конструктор по умолчанию
        /// </summary>
        /// <param name="function">Функция, требующая решения</param>
        /// <param name="a">Нижняя граница интервала</param>
        /// <param name="b">Верхняя граница интервала</param>
        /// <param name="pointsNumber">Число точек</param>
        public TrapeziumIntegration(FunctionWithOneArgument function, double a, double b, int pointsNumber)
        {
            double h = 0;
            double j = 0 ;
            double stepResult = 0;
            int she = 4;            
            _result = new double[2, pointsNumber+1];
            for (int i = 0; i <= pointsNumber; i++)
            {
                h = (double)(b - a) / (double)she;
                she += 4;
                stepResult = function(a) + function(b);
                for (j = a + h; j < b; j = j + h)
                {
                    stepResult = stepResult + 2 * function(j);
                }
                stepResult = stepResult * h / 2;
                _result[0,i] = stepResult;
                _result[1,i] = h;
            }
        }

        /// <summary>
        /// Возвращает решение
        /// </summary>
        /// <returns>Equation solution</returns>
        public double[,] GetSolution()
        {
            return _result;
        }
    }
}
